package com.android.videotest;

import android.Manifest;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.util.Pair;
import android.view.KeyEvent;
import android.view.KeyboardShortcutGroup;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.android.videotest.activity.BannerActivity;
import com.android.videotest.activity.DialogActivity;
import com.android.videotest.activity.FilesActivity;
import com.android.videotest.activity.JiaoziPlayerActivity;
import com.android.videotest.activity.MatrixTestActivity;
import com.android.videotest.activity.RunnableActivity;
import com.android.videotest.activity.SimpleVideoActivity;
import com.android.videotest.activity.TextureViewActivity;
import com.android.videotest.activity.TranslateActivity;
import com.android.videotest.activity.VideoViewActivity;
import com.android.videotest.activity.VideoViewActivity2;
import com.android.videotest.activity.ijkDemoActivity;
import com.android.videotest.music.MusicLibrary;
import com.android.videotest.music.ui.MediaPlayerActivity;
import com.android.videotest.music.ui.SimpleMusicActivity;
import com.android.videotest.music.ui.musiclist.MusicListActivity;
import com.android.videotest.util.ClazzUtils;
import com.android.videotest.video.VideoActivity;
import com.android.videotest.video.listvideo.VideoListActivity;

import java.util.ArrayList;
import java.util.List;

public class ListActivity extends AppCompatActivity {
    private static final String TAG = "ListActivity-App";
    private ListActivity mListActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        mListActivity = this;
        List<String> list = ClazzUtils.getClazzName("com.android.videotest.activity", false);
        Log.d(TAG, "onCreate:string " + list);
//        for (String string : list) {
//            System.out.println(string);
//        }
        if (ContextCompat.checkSelfPermission(ListActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(ListActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
            Log.d(TAG, "onCreate: 请求权限");
            Log.d(TAG, "onCreate:请求权限 ");
        } else {
            new Thread(() -> {
                MusicLibrary.initSongList(this);
            }).start();
            Log.d(TAG, "onCreate: 有权限");
            Log.d(TAG, "onCreate:有权限 ");
        }

        ArrayList<Pair<String, View.OnClickListener>> strings = new ArrayList<>();
        strings.add(new Pair<>("FilesActivity", v -> FilesActivity.start(mListActivity)));
        strings.add(new Pair<>("VideoViewActivity2", v -> {
//            Uri uri = Uri.parse("android.resource://com.android.videotest/" + R.raw.test_vide);
//            VideoViewActivity2.start(mListActivity, uri);
        }));
        strings.add(new Pair<>("MatrixTestActivity", v -> MatrixTestActivity.start(mListActivity)));
        strings.add(new Pair<>("TranslateActivity", v -> TranslateActivity.start(mListActivity)));
        strings.add(new Pair<>("BannerActivity", v -> BannerActivity.start(mListActivity)));
        strings.add(new Pair<>("RunnableActivity", v -> RunnableActivity.start(mListActivity)));
        strings.add(new Pair<>("JiaoziPlayerActivity", v -> JiaoziPlayerActivity.start(mListActivity)));
        strings.add(new Pair<>("系统控件", null));
        strings.add(new Pair<>("VideoViewActivity", v -> VideoViewActivity.start(mListActivity)));
        strings.add(new Pair<>("HtdSimpleVideoActivity", v -> SimpleVideoActivity.start(mListActivity)));
        strings.add(new Pair<>("TextureViewActivity", v -> TextureViewActivity.start(mListActivity)));
        strings.add(new Pair<>("ijkDemoActivity", v -> ijkDemoActivity.start(mListActivity)));
        strings.add(new Pair<>("DialogActivity", v -> DialogActivity.start(mListActivity)));
        strings.add(new Pair<>("MediaPlayerActivity", v -> MediaPlayerActivity.start(mListActivity)));
        strings.add(new Pair<>("MusicListActivity", v -> MusicListActivity.start(mListActivity)));
        strings.add(new Pair<>("SimpleMusicActivity", v -> SimpleMusicActivity.start(mListActivity)));
        strings.add(new Pair<>("VideoActivity", v -> VideoActivity.start(mListActivity, "")));
        strings.add(new Pair<>("VideoListActivity", v -> VideoListActivity.start(mListActivity)));
        strings.add(new Pair<>("VideoListActivity", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick() called with: v = [" + v + "]");
                VideoListActivity.start(mListActivity);
            }
        }));

        ListView listView = findViewById(R.id.listView);
        listView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return strings.size();
            }

            @Override
            public Object getItem(int position) {
                return strings.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                Pair pair = (Pair) getItem(position);
                if (convertView == null) {
//                    AbsListView.MarginLayoutParams layoutParams = new AbsListView.MarginLayoutParams(-1, -2);
//                    layoutParams.setMargins(100,100,100,100);
                    Pair<String, View.OnClickListener> item = (Pair<String, View.OnClickListener>) getItem(position);
                    convertView = new TextView(mListActivity);
                    convertView.setFocusable(true);
                    convertView.setBackgroundResource(R.drawable.card_selector_rectangle);
                    convertView.setClickable(true);
                    if (item.second == null) {
                        convertView.setPadding(16, 8, 16, 8);
                    } else {
                        convertView.setPadding(30, 30, 30, 30);
                    }
//                    convertView.setLayoutParams(layoutParams);
                }
                ((TextView) convertView).setText((String) pair.first);
                convertView.setOnClickListener((View.OnClickListener) pair.second);
                return convertView;
            }
        });

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.d(TAG, "onKeyDown() called with: keyCode = [" + keyCode + "], event = [" + event + "]");
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
        Log.d(TAG, "onKeyMultiple() called with: keyCode = [" + keyCode + "], repeatCount = [" + repeatCount + "], event = [" + event + "]");
        return super.onKeyMultiple(keyCode, repeatCount, event);
    }

    @Override
    public boolean onKeyShortcut(int keyCode, KeyEvent event) {
        Log.d(TAG, "onKeyShortcut() called with: keyCode = [" + keyCode + "], event = [" + event + "]");
        return super.onKeyShortcut(keyCode, event);

    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        Log.d(TAG, "onKeyUp() called with: keyCode = [" + keyCode + "], event = [" + event + "]");
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public void onProvideKeyboardShortcuts(List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
        Log.d(TAG, "onProvideKeyboardShortcuts() called with: data = [" + data + "], menu = [" + menu + "], deviceId = [" + deviceId + "]");
        super.onProvideKeyboardShortcuts(data, menu, deviceId);
    }

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        Log.d(TAG, "onKeyLongPress() called with: keyCode = [" + keyCode + "], event = [" + event + "]");
        return super.onKeyLongPress(keyCode, event);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.d(TAG, "onRequestPermissionsResult: 已授予权限");
                    new Thread(() -> {
                        MusicLibrary.initSongList(this);
                    }).start();
                } else {
                    Toast.makeText(this, "You denied the permission", Toast.LENGTH_SHORT).show();
                    Log.d(TAG, "onRequestPermissionsResult: 禁止权限");
                }
                break;
            default:
        }
    }

}
